/*
 * Created on 2005-2-24
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ims.iaa;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Timestamp;

/**
 * @author zhufx
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SpecialExceptionLog {

	/**
	  * The PrintStream used to save log information
	  */
	private PrintStream ps = null;
	private static SpecialExceptionLog m_instance = new SpecialExceptionLog();
	
	public static SpecialExceptionLog getInstance(){
		return m_instance;
	}
	
	private SpecialExceptionLog(){
		createLogFile();
	}

	public void println(String x){
		String time = new Timestamp(System.currentTimeMillis()).toString();
		ps.println(time + " : " + x);
		//ps.print(getWhere());
	}

	public void printStackTrace(Throwable throwable){
		String time = new Timestamp(System.currentTimeMillis()).toString();
		ps.println(time + ": ");
		throwable.printStackTrace(ps);
	}
 
 	private void createLogFile() {
		String fileName = getFileName();
		String tempName = fileName;

		//26 Oct 2000 Anthony Zhang		
		//check the log directory property and store log file 
		//under the specified directory
		if (!IaaDebug.LogDirectory.equals("")) {
		    //if(IaaDebug.Debug) System.out.println("Create log directory" + IaaDebug.LogDirectory);
		 	File path = new File(IaaDebug.LogDirectory);
 			try  {
 				if (!path.exists())	path.mkdirs();
				//if we can successfully create the directory than we can store info 
				//there. 
			 	fileName = IaaDebug.LogDirectory + "\\" + fileName;
		 	} catch (SecurityException e)  {
 				IaaUtil.log("Creat dir error " + path + e);			
	 		}		
		}
		
		try  {
			ps = new PrintStream( new FileOutputStream(fileName, true));
		} catch (IOException e ) {
			System.out.println( "Create log file failed " + fileName + " :" + e );
			try  {
				ps = new PrintStream( new FileOutputStream(tempName));
			} catch (IOException ex ) {
				System.out.println( "Fatal error: create log file failed " + tempName + " :" + ex );
				ps = System.out;
			}
		}
 	}
	
	private String getFileName() {
	    String sRet = "";
		try  {
		    sRet = java.net.InetAddress.getLocalHost().getHostName();
		} catch (Exception e)  {
			IaaUtil.log("Error get localhost name" + e);
			sRet = "Unknown";
		}
		sRet += "_Log_Special.log";
		
		return sRet;
	}
    private static String getWhere()
    {
        String where = "";
        Thread currentThread = Thread.currentThread();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        PrintStream pw = new PrintStream(os);
        System.setErr(pw);
       	Thread.dumpStack();
        System.setErr(System.err);
        pw.flush();
        where = os.toString();
        
        return where;
        /*
        // Find the last method called before Debug's static methods.
        StringTokenizer st = new StringTokenizer(text, "\n");
        while (st.hasMoreTokens())
        {
            String s = st.nextToken();
            if (s.indexOf("com.bluejaynetworks.InterConnAPI.GscDiag") == -1 &&
                s.indexOf("java.lang.Thread") == -1 &&
                s.indexOf("at ") > -1)
            {
                where = s.trim();
                break;
            }
        }
        pw.close();
        
        return where;*/
    }
	
	private void closeLogFile()  {
		ps.close();
	}
}
